require 'ftools'
require 'Read_Properties'
	propertyhash = load_properties(".//test.properties")
	OriginalSahiFileDir=propertyhash["OriginalSahiFileDir"]
	NewSahiFileDir=propertyhash["NewSahiFileDir"]
	DataFileDir=propertyhash["DataFileDir"]
	toformatFile=propertyhash["toformatFile"]
	stringarr=Array.new 
	File.open(toformatFile,"r") do |myfile|
	myfile.each_line {|parseline|
	stringarr.push(parseline)
	}
	end	

	Dir.foreach(OriginalSahiFileDir){|filename|
   	if (filename=~/sah/) 
 		File.open(OriginalSahiFileDir+filename,"r") do |myfile|
 		name="Name"
 		value="Value"
 		counter=0
 		rowstring=""
 		scriptstring="_log(\"Function Started : " +filename.gsub(".sah","")+"\",\"success\"); \n"
 		
 		functionstring=""		
   		 	myfile.each_line {|parseline|
 			myarr=parseline.split('"')
 			notmatched=true
 			stringarr.each{|element|
 			elementarr=element.split('"')
 			if (elementarr[0]== myarr[0])
 				notmatched=false
 			#	puts "$$$$$$$$$$$$" +  parseline
 				for i in 0..(myarr.length-1)
				 case elementarr[i]
					when "PARAM" then 
						scriptstring = scriptstring + "$" + myarr[i].gsub('.','').gsub(" ","_")  
						name=name+","+myarr[i].gsub(".","").gsub(" ","_")
						value=value+ "," +myarr[i].gsub(",","##comma##")
						if(rowstring!="")
   		 					rowstring=rowstring+",$row["+(counter=counter+1).to_s+"]"+".replace('##comma##',',')"
   		 				else
   		 					rowstring=rowstring+"$row["+(counter=counter+1).to_s+"]"+".replace('##comma##',',')"
   		 				end	
					when "NOPARAM" then scriptstring = scriptstring + "\"" + myarr[i] + "\""
						
					when "PARAMPREV" then scriptstring = scriptstring + "$" + myarr[i-2].gsub('.','').gsub(" ","_").gsub("-","")
						name=name+","+myarr[i-2].gsub(".","").gsub(" ","_").gsub("-","")
						value=value+ "," +myarr[i].gsub(",","##comma##")
						if(rowstring!="")
   		 					rowstring=rowstring+",$row["+(counter=counter+1).to_s+"]"+".replace('##comma##',',')"
   		 				else
   		 					rowstring=rowstring+"$row["+(counter=counter+1).to_s+"]"+".replace('##comma##',',')"
   		 				end	
					else scriptstring = scriptstring+ myarr[i]
					end	
					
 				end	
 			end	
			}
			if (notmatched)
				 scriptstring = scriptstring+ parseline
				
			end	 	
  			}	
  			
  			 		 scriptstring = scriptstring+ "\n_log(\"Function Ended  :" +filename.gsub(".sah","")+"\",\"success\");"
  			 		 
  		if(name=="Name")
  			File.open(NewSahiFileDir+filename, 'w') {|f| f.write("function "+filename.gsub(".sah","")+"(){ \n try{\n") }	
  		else
  			File.open(NewSahiFileDir+filename, 'w') {|f| f.write("function "+filename.gsub(".sah","")+"($"+name.gsub(/^Name,/,"").gsub(",",",$")+"){ \n try{\n") }
  		end		

		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(scriptstring) }			
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write("\n}catch(e){\n takeSnapShot();\n_logExceptionAsFailure(e);\n}\n}") }	
		File.open(DataFileDir+filename.gsub('sah','csv'), 'w') {|f| f.write(name+"\n"+value) }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write("\n function "+filename.gsub(".sah","")+"_($csvfile,$rowvalue){ \n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" try{\n 	  var $datafound=false;\n 	  var $temp= 	 _userDataDir();   \n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" var $filepath=$csvfile\n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" var $data = _readCSVFile($filepath); \n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" for (var $i=1; $i<$data.length; $i++){\n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" 	var $row = $data[$i];\n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" 	if($row[0]==$rowvalue)\n{\n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(filename.gsub(".sah","")+"("+rowstring+");\n $datafound=true; \n
break;\n}\n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write(" 	else \n 	{\n}\n}\n") }
		File.open(NewSahiFileDir+filename, 'a') {|f| f.write("  	                if($datafound==false)\n                {\n                var $errmessage=\"NO DATA ROW : \"+$rowvalue+\" FOUND FOR : \"+$csvfile; \n                _log($errmessage, \"failure\"); \n                }\n}catch(e){ \n  		 takeSnapShot();\n		_logExceptionAsFailure(e);\n 	}\n}\n") }
	
  			
  			puts scriptstring	 		
 		end
	end
 }
